import sys

import pygame

from surface.text import Text


class Menu(object):
    _title = '坦克大战'
    step = 65

    def __init__(self, setting, state):
        self._settings = setting
        self._state = state
        # 游戏标题
        self.title = Text(self._title, 150)
        self.title_rect = self.title.surface.get_rect(centerx=self._settings.screen_width / 2)
        # 游戏菜单
        self.menu_image = pygame.image.load('images/select.gif')
        self.menus_rect = self.menu_image.get_rect(centerx=self.title_rect.centerx, top=self.title_rect.bottom + 50)
        # 坦克
        self.select_image = pygame.image.load('images/selecttank.gif')
        self.select_rect = self.select_image.get_rect(top=self.menus_rect.top - 8, left=self.menus_rect.left - 60)
        self.curent = 0

    def display(self, screen):
        self.listen_event()
        self.draw(screen)

    def draw(self, screen):
        self.title.display(screen, self.title_rect)
        screen.blit(self.menu_image, self.menus_rect)
        screen.blit(self.select_image, self.select_rect)

    def listen_event(self):
        for event in pygame.event.get():
            if event.type == pygame.KEYUP:
                if event.key == pygame.K_UP:
                    if self.curent == 0:
                        self.select_rect.centery += 2 * self.step
                        self.curent = 2
                    else:
                        self.select_rect.centery -= self.step
                        self.curent -= 1
                if event.key == pygame.K_DOWN:
                    if self.curent == 2:
                        self.select_rect.centery -= 2 * self.step
                        self.curent = 0
                    else:
                        self.select_rect.centery += self.step
                        self.curent += 1

                if event.key == pygame.K_SPACE and self.curent == 0:
                    self._state.active = True

            if event.type == pygame.QUIT:
                print('stop game')
                sys.exit()
